Sring Cloud Sentinel

By youfang

Sentinel

Sentinel 介绍

Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。

控制台

启动命令:

1
2
3
4
5
6
7
# 1
D:\developer\sentinal>java -Dserver.port=9100 -Dcsp.sentinel.dashboard.server=localhost:9100 -Dproject.name=sentinel-console -jar ./sentinel-dashboard-1.7.2.jar
# 2
D:\developer\sentinal>java -Dserver.port=9100 -Dcsp.sentinel.dashboard.server=localhost:9100 -Dsentinel.dashboard.auth.username=sentinel -Dsentinel.dashboard.auth.password=sentinel123 -Dserver.servlet.session.timeout=7200 -Dproject.name=sentinel-console -jar ./sentinel-dashboard-1.7.2.jar
# Linux
[root@acyou sentinel]# nohup java -Dserver.port=9100 -Dcsp.sentinel.dashboard.server=localhost:9100 -Dproject.name=sentinel-console -jar ./sentinel-dashboard-1.7.2.jar >./log.out 2>&1 &

启动完成:

访问地址:http://localhost:9100/#/dashboard/home

控制台的集成SpringBoot

  1. bootstrap.properties文件
    1
    2
    3
    4
    # 启用Sentinel控制台
    spring.cloud.sentinel.transport.dashboard=localhost:9100
    spring.cloud.sentinel.transport.port=8719

  2. 注意事项
    • 首次访问资源时,才会初始化并给控制台发送心跳,之后控制台会通过接入端提供的端口对 Sentinel 进行访问来拉取相关信息。

Sentinel与Gateway的集成

注意

  1. 启动项目的时候增加JVM参数,以标记为GateWay。-Dcsp.sentinel.app.type=1
  2. Sentinel控制台与Gateway必须互通。不能一个用外网、一个用内网!
  3. 注意Maven包的引用,使用控制台的时候。有个包:sentinel-transport-simple-http
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    <!-- alibaba sentinel 网关适配 start -->
    <dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
    </dependency>
    <dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-transport-simple-http</artifactId>
    </dependency>
    <!-- 网关限流一直实现不了,https://github.com/alibaba/Sentinel/issues/1306 -->
    <!-- 解答:
    如果使用SCA的spring-cloud-alibaba-sentinel-gateway,
    因为它同时集成了zuul和spring cloud gateway,这两个依赖是可选的(optional),
    需要使用方显示地加上依赖。如使用spring cloud gateway,加上依赖spring-cloud-starter-gateway。
    -->
    <dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-parameter-flow-control</artifactId>
    </dependency>
    <!-- alibaba sentinel 网关适配 end-->